import 'dart:io';

import 'package:example/app/config/http_config.dart';
import 'package:example/app/config/res_config.dart';
import 'package:example/app/routes/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_base_framework/base_library.dart';
import 'package:flutter_base_framework/flutter_base_framework.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:oktoast/oktoast.dart';

import 'app/modules/splash_module/splash_bindings.dart';
import 'app/modules/splash_module/splash_page.dart';
import 'app/translations/app_translations.dart';
import 'app/utils/dependency_injection.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await DenpendencyInjection.init();
  // 基础库初始化
  BaseLibrary.init(isDebug: true, httpConfig: HttpConfig(), resConfig: ResConfig());
  SystemChrome.setPreferredOrientations(
          [DeviceOrientation.portraitDown, DeviceOrientation.portraitUp])
      .then((value) {
    runApp(MyApp());
    if (Platform.isAndroid) {
      // Android状态栏透明 splash为白色, 所以调整状态栏文字为黑色
      SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
          statusBarColor: Colors.transparent,
          statusBarBrightness: Brightness.light));
    }
  });
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    setDesignWHD(375, 812);
    LogUtil.init(isDebug: true);
    return OKToast(
      dismissOtherOnShow: true,
      radius: 6.0,
      movingOnWindowChange: false,
      textPadding: EdgeInsets.all(10),
      backgroundColor: Color(0x80000000),
      child: RefreshConfiguration(
        hideFooterWhenNotFull: true, //列表数据不满一页,不触发加载更多
        child: GetMaterialApp(
          defaultTransition: Transition.cupertino,
          opaqueRoute: Get.isOpaqueRouteDefault,
          popGesture: Get.isPopGestureEnable,
          debugShowCheckedModeBanner: false,
          theme: ThemeData(brightness: Brightness.light),

          /// 多语言配置, 此处强制用中文
          locale: Locale('zh', 'CN'),
          localizationsDelegates: const [
            RefreshLocalizations.delegate, //下拉刷新
            GlobalCupertinoLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
          ],
          supportedLocales: [
            Locale('zh', 'CN'),
            Locale('en', 'US'),
          ],
          translationsKeys: AppTranslation.translations,
          initialRoute: Routes.INITIAL,
          getPages: AppPages.pages,
          home: SplashPage(),
          initialBinding: SplashBinding(),
          builder: (context, child) => Scaffold(
            body: GestureDetector(
              onTap: () => Get.hideKeyboard(context),
              child: child,
            ),
          ),
        ),
      ),
    );

  }
}
